package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.aspect.annotation.OperLog;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @return R
     */
    //--T1-1：登录
    @PostMapping("/login")
    @OperLog(value = "员工登录")
    public R login(@RequestBody Employee employee, HttpSession session){
        log.info("[》》》PostMapping--login]:用户登录：-------------------");
        log.info("[》》》PostMapping--employee]:{}",employee);

        //-1-接收Json对象，送到业务层___验证：名称，密码是否一致
        //_____返回结果用Map----msg：信息  employee：对象
        Map loginResultMap = employeeService.checkLogin(employee);
        Object loginEmp = loginResultMap.get("employee");

        //-2- Map没有对象，则返回错误信息
        if (loginEmp==null){
            return R.error((String)loginResultMap.get("msg"));
        }
        //-3- Map没有对象--即登录成功__封装到session中
        session.setAttribute("employee",loginEmp);
        return R.success(loginEmp);
    }

    /**
     * 用户退出
     */
    //--T1-2：退出
    @PostMapping("/logout")
    public R logout(HttpSession session){
        //用户成功之后，将用户信息存储到session中
        //用户退出之后，将存储到session中的用户信息删除
        log.info("[》》》PostMapping--logout]:用户退出：-------------------");
        session.removeAttribute("employee");
        log.info("[》》》PostMapping--logout]:用户退出：{}",session.getAttribute("employee"));
        return R.success("退出成功");
    }

    //--T2-1：添加员工
    @OperLog(value = "添加员工",useranme = "admin")
    @PostMapping
    public R addEmployee(@RequestBody Employee employee,HttpSession session){
        Employee admin = (Employee)session.getAttribute("employee");
        boolean result = employeeService.addEmployee(employee, admin);

        return result?R.success("新增员工成功"):R.error("新增员工失败");
    }

    //--T2-2：分页查询---- /employee/page?page=1&pageSize=10&name=zs
    @GetMapping("/page")
    public R selectByPage(@RequestParam Map map){
        log.info("[>>>--<员工模块>分页查询]: map={}",map);
        IPage<Employee> employeeIPage=employeeService.queryPage(map);
        return R.success(employeeIPage);
    }

    //--T2-3：员工-禁用/启用------    T2-4:修改
    @PutMapping
    public R updataEmployee(@RequestBody Employee employee,HttpSession session){
        //管理员id
        Employee admin = (Employee)session.getAttribute("employee");
        boolean result = employeeService.updataEmployee(employee, admin);

        return result?R.success("员工信息修改成功"):R.error("员工信息修改失败");
    }


    //--T2-4-1：找id
    //请求url地址： /employee/1503342331818840066
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        Employee employee = employeeService.selectByIdEmployee(id);
        if(employee!=null)
            return R.success(employee);
        else
            return R.error("当前id不存在");
    }




}
